package com.ykdl.net.manager;

import net.wxxr.http.interfaces.IAddCommonHeaders;
import net.wxxr.http.interfaces.IAddCommonParams;
import net.wxxr.http.interfaces.IGetLoginoutData;
import net.wxxr.http.interfaces.INetStatus;
import net.wxxr.http.interfaces.IResponseHeaders;
import net.wxxr.http.interfaces.ISaveUserInfoForAutoLoginSuccess;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import net.wxxr.thread.pool.RunnablePool;
import net.wxxr.thread.pool.Task;
import net.wxxr.thread.pool.TaskCommond;
import net.wxxr.thread.pool.ThreadGroup;
import oauth.signpost.http.HttpParameters;

import org.apache.http.HttpHost;

import android.webkit.WebView;

import com.ykdl.member.KidApp;
import com.ykdl.member.base.GlobalContext;
import com.ykdl.member.base.QLog;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.util.AccessTokenKeeper;
import com.ykdl.member.kid.util.ApnUtil;
import com.ykdl.net.response.HttpDataResponse;
/**
 * 
 * 网络任务的管理类
 *
 * @since 1.0
 */
public class TaskManager {
	static {
		HttpBaseRequest.setAddCommonParams(new IAddCommonParams() {
			//添加url 公用参数
			@Override
			public void addCommonParams(HttpParameters urlParams) {
//				urlParams.put(AccessTokenKeeper.getHTTPTITLE_TOKEN_TITLE(),AccessTokenKeeper.getHTTPTITLE_TOKEN());
			}
		});
		//添加公用头信息
		HttpBaseRequest.setAddCommonHeaders(new IAddCommonHeaders() {
			
			@Override
			public void addCommonHeaders(HttpParameters headers) {
				// TODO Auto-generated method stub
				String userAgentString;
				try {
					userAgentString = System.getProperties().getProperty("http.agent");
				} catch (Exception e) {
					userAgentString = "android-agent";
				}
//				channel = 91;
//				deviceid = aL3uNGkggTDcWLSDZvZHgE;
//				 osVersion = "7.1";
//				platform = iphone;
//				screen = 640x960;
//				ver = "1.0.0";

				headers.put("User-Agent", userAgentString);				
				headers.put("Accept", "*/*");//服务器自己接受什么介质类型
				headers.put("deviceId", GlobalContext.getDeviceID());
				headers.put("push_token", GlobalContext.getDeviceID());
				headers.put("ver", GlobalContext.getVersionCode());
				headers.put("screen", KidApp.SCREEN_WIDTH+"x"+KidApp.SCREEN_HEIGHT);
				headers.put("osVersion", android.os.Build.VERSION.SDK+"");
				headers.put("platform", "android");
				
				
			}
		});
		
		
		
		
		/**
		 * 设置代理服务器
		 */
		HttpHost httpHost = ApnUtil.getHttpHost();
		HttpBaseRequest.setHttpHost(httpHost);
		
		
		//用户认证,判断用户是否已注册，
		/*HttpBaseRequest.setCheckUser(new ICheckUser() {
			
			@Override
			public boolean isUserLegal(boolean needAuth) {
				 //这里不做用户的签名认证，只判断当前用户是否登录
				if(needAuth && !KidApp.getInstance().isRegisted()) {
					return false;
				}
				return true;
			}
		});*/
		
		
		//用户认证,判断用户是否已注册，这里我们不需要这一功能验证,设置为null
		HttpBaseRequest.setCheckUser(null);
		
		
		//查看网络是否可用状态
		HttpBaseRequest.setNetStatus(new INetStatus() {
			@Override
			public boolean isNetAvaliable() {
				if(ApnUtil.isActivityApnConnected(KidApp.getInstance().getApplicationContext())) {
					return true;
				}
				return false;
			}
		});
		
		
		/**
		 * 设置请求响应报文头信息，这里针对于用户登录时，获得Set-Cookie 中的jsession值
		 */
		HttpBaseRequest.setResponseHeaders(new IResponseHeaders() {
			@Override
			public void settingResponseHeaders(String jsession,String data) {
				
			}
		});
		
		
		
		
		
	}
	
	
	
	
	
	
	@SuppressWarnings("rawtypes")
	private static ThreadGroup dataRequestPool;
	
	/**
	 * 文本信息的请求
	 * @param request 请求对象基类
	 * @param tag //请求模块的Tag标识
	 * @param clazz //操作实体类(对json数据的解析并封装成实体类。)
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void startHttpRequest(HttpBaseRequest request, ITag tag, Class clazz) {
		if(dataRequestPool == null) {
			//创建一个线程池，核心线程池为2，等待线程池为20
			dataRequestPool = new ThreadGroup(2, 20);
		}
		TaskCommond commond = new TaskCommond();
		Task task = new Task(request, tag, clazz);
		commond.setResponse(new HttpDataResponse());
		commond.setTask(task);
		dataRequestPool.addTask(commond);
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void syncStartHttpRequest(HttpBaseRequest request, ITag tag, Class clazz) {
		if(dataRequestPool == null) {
			//创建一个线程池，核心线程池为1，等待线程池为20
			dataRequestPool = new ThreadGroup(1, 20);
		}
		TaskCommond commond = new TaskCommond();
		Task task = new Task(request, tag, clazz);
		commond.setResponse(new HttpDataResponse());
		commond.setTask(task);
		dataRequestPool.addTask(commond);
	}
	
	
	/**
	 * 文本信息的（如json格式）请求，增加了TaskCommond对象，对服务器响应的json进行自定义的类。
	 * @param request 请求对象基类
	 * @param tag //请求模块的Tag标识
	 * @param clazz //操作实体类(对json数据的解析并封装成实体类。)
	 * @param commond //处理实体类业务逻辑类。(如 缓存数据)
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void startHttpRequestWithTaskCommon(HttpBaseRequest request, ITag tag, Class clazz, TaskCommond commond) {
		
		if(dataRequestPool == null) {
			dataRequestPool = new ThreadGroup(4, 20);
		}
		Task task = new Task(request, tag, clazz);
		commond.setResponse(new HttpDataResponse());
		commond.setTask(task);
		dataRequestPool.addTask(commond);
		
	}
	
	/**
	 * 文件上传请求。如上传或者发送图片
	 * @param request
	 * @param tag
	 * @param clazz
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void uploadImageRequest(HttpBaseRequest request, ITag tag, Class clazz){
		if(dataRequestPool == null) {
			//创建一个线程池，核心线程池为2，等待线程池为20
			dataRequestPool = new ThreadGroup(1, 20);
		}
		TaskCommond commond = new TaskCommond();
		Task task = new Task(request, tag, clazz);
		commond.setResponse(new HttpDataResponse());
		commond.setTask(task);
		dataRequestPool.addTask(commond);
		
	}
	
	/**
	 * 数据的发送或上传 (和 startHttpRequest用法一致，只是名字不一样。方便调用)
	 * @param request
	 * @param tag
	 * @param clazz
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void uploadDataRequest(HttpBaseRequest request, ITag tag, Class clazz){
		if(dataRequestPool == null) {
			//创建一个线程池，核心线程池为2，等待线程池为20
			dataRequestPool = new ThreadGroup(1, 20);
		}
		
		TaskCommond commond = new TaskCommond();
		Task task = new Task(request, tag, clazz);
		commond.setResponse(new HttpDataResponse());
		commond.setTask(task);
		dataRequestPool.addTask(commond);
		
	}
	
	public static void startRunnableRequest(Runnable runnable) {
		RunnablePool.getInstance().addTask(runnable);
	}
}
